<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="180px">
      <el-form-item label="姓名" prop="jobName">
        <el-input v-model="dataForm.jobName" placeholder="姓名"></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="jobMobile">
        <el-input v-model="dataForm.jobMobile" placeholder="手机"></el-input>
      </el-form-item>
      <el-form-item label="微信/QQ/微博" prop="jobWqw">
        <el-input v-model="dataForm.jobWqw" placeholder="微信/QQ/微博"></el-input>
      </el-form-item>
      <el-form-item label="居住地址" prop="jobAddr">
        <el-input v-model="dataForm.jobAddr" placeholder="居住地址"></el-input>
      </el-form-item>
      <el-form-item label="应聘岗位" prop="jobPosition">
        <el-input v-model="dataForm.jobPosition" placeholder="应聘岗位"></el-input>
      </el-form-item>
      <el-form-item label="对未来岗位和公司的期待" prop="jobFuture">
        <el-input type="textarea" v-model="dataForm.jobFuture" placeholder="对未来岗位和公司的期待"></el-input>
      </el-form-item>
      <el-form-item label="薪资要求" prop="jobXz">
        <el-input v-model="dataForm.jobXz" placeholder="薪资要求"></el-input>
      </el-form-item>
      <el-form-item label="到岗时间" prop="jobGetTime">
        <el-input v-model="dataForm.jobGetTime" placeholder="到岗时间"></el-input>
      </el-form-item>
      <el-form-item label="上传简历" prop="jobJl">
        <el-row :gutter="18">
          <el-col :span="17">
            <el-input v-model="dataForm.jobJl" placeholder="公司介绍"></el-input>
          </el-col>
          <el-col :span="1" class="login-captcha">
            <el-button type="primary" @click="uploadHandle()">上传文件</el-button>
          </el-col>
        </el-row>
      </el-form-item>
      <el-form-item label="备注" prop="jobRemarks">
        <el-input v-model="dataForm.jobRemarks" placeholder="备注"></el-input>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()"    @refreshDataList="updateNickName">确定</el-button>
    </span>
    <!-- 弹窗, 上传文件 -->
    <upload v-if="uploadVisible" ref="upload"  @refreshDataList="updateNickName"></upload>
  </el-dialog>
</template>

<script>
  import API from '@/api'
  import Upload from './upload'
  export default {
    data () {
      return {
        visible: false,
        uploadVisible: false,
        dataForm: {
          id: 0,
          userId: '',
          jobName: '',
          jobMobile: '',
          jobWqw: '',
          jobAddr: '',
          jobPosition: '',
          jobFuture: '',
          jobXz: '',
          jobGetTime: '',
          jobJl: '',
          jobRemarks: '',
          createUserId: '',
          createTime: ''
        },
        dataRule: {
          userId: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          jobName: [
            { required: true, message: '姓名不能为空', trigger: 'blur' }
          ],
          jobMobile: [
            { required: true, message: '手机不能为空', trigger: 'blur' }
          ],
          jobWqw: [
            { required: true, message: '微信/QQ/微博不能为空', trigger: 'blur' }
          ],
          jobAddr: [
            { required: true, message: '居住地址不能为空', trigger: 'blur' }
          ],
          jobPosition: [
            { required: true, message: '应聘岗位不能为空', trigger: 'blur' }
          ],
          jobFuture: [
            { required: true, message: '对未来岗位和公司的期待不能为空', trigger: 'blur' }
          ],
          jobXz: [
            { required: true, message: '薪资要求不能为空', trigger: 'blur' }
          ],
          jobGetTime: [
            { required: true, message: '到岗时间不能为空', trigger: 'blur' }
          ],
          jobJl: [
            { required: true, message: '上传简历不能为空', trigger: 'blur' }
          ],
          jobRemarks: [
            { required: true, message: '备注不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    components: {
      Upload
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            API.basejob.info(this.dataForm.id).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.userId = data.basejob.userId
                this.dataForm.jobName = data.basejob.jobName
                this.dataForm.jobMobile = data.basejob.jobMobile
                this.dataForm.jobWqw = data.basejob.jobWqw
                this.dataForm.jobAddr = data.basejob.jobAddr
                this.dataForm.jobPosition = data.basejob.jobPosition
                this.dataForm.jobFuture = data.basejob.jobFuture
                this.dataForm.jobXz = data.basejob.jobXz
                this.dataForm.jobGetTime = data.basejob.jobGetTime
                this.dataForm.jobJl = data.basejob.jobJl
                this.dataForm.jobRemarks = data.basejob.jobRemarks
                this.dataForm.createUserId = data.basejob.createUserId
                this.dataForm.createTime = data.basejob.createTime
              }
            })
          }
        })
      },
      handleChange (val) {
        console.log(val)
      },
      updateNickName (supplierCompanyDesc) {
        this.dataForm.jobJl = supplierCompanyDesc
      },
      // 上传文件
      uploadHandle () {
        this.uploadVisible = true
        this.$nextTick(() => {
          this.$refs.upload.init()
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            var params = {
              'id': this.dataForm.id || undefined,
              'userId': this.dataForm.userId,
              'jobName': this.dataForm.jobName,
              'jobMobile': this.dataForm.jobMobile,
              'jobWqw': this.dataForm.jobWqw,
              'jobAddr': this.dataForm.jobAddr,
              'jobPosition': this.dataForm.jobPosition,
              'jobFuture': this.dataForm.jobFuture,
              'jobXz': this.dataForm.jobXz,
              'jobGetTime': this.dataForm.jobGetTime,
              'jobJl': this.dataForm.jobJl,
              'jobRemarks': this.dataForm.jobRemarks,
              'createUserId': this.dataForm.createUserId,
              'createTime': this.dataForm.createTime
            }
            var tick = !this.dataForm.id ? API.basejob.add(params) : API.basejob.update(params)
            tick.then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
